import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import AutoImport from 'unplugin-auto-import/vite'
import Components from 'unplugin-vue-components/vite'
import { ElementPlusResolver } from 'unplugin-vue-components/resolvers'
import { resolve } from 'path'
import Unocss from "unocss/vite";
import { presetAttributify, presetUno, transformerDirectives } from "unocss";
import vueSetupExtend from "unplugin-vue-setup-extend-plus/vite";

// https://vitejs.dev/config/
export default defineConfig({
    resolve: {
        alias: {
            '@': resolve(__dirname, './src'),
            'vue-i18n': 'vue-i18n/dist/vue-i18n.cjs.js',
        }
    },
    define: {
        'process.env': { ...process.env }
      },
    plugins: [
        vue(),
        //name可以写在setup中
        vueSetupExtend({}),
        AutoImport({
            resolvers: [ElementPlusResolver()],

            include: [
                /\.[tj]sx?$/, // .ts, .tsx, .js, .jsx
                /\.vue$/, /\.vue\?vue/, // .vue
            ],
            imports: [
                'vue',
                'pinia'
            ],
            dirs: [
                './src/type/components/',
                './src/utils/',
                './src/components/icon',
                './src/components/proTable'
            ],
            dts: 'src/auto-import.ts',
            vueTemplate: true,
        }),
        Components({
            resolvers: [
                ElementPlusResolver()
            ],
        }),
        Unocss({
            presets: [presetAttributify({}), presetUno()],
            rules: [
                [/^fs-(\d+\.{0,1}\d{0,2})$/, ([, d]) => ({ "font-size": `${d}px` })],
                [/^ml(\d+\.{0,1}\d{0,2})$/, ([, d]) => ({ "margin-left": `${d}px` })],
                [/^mr(\d+\.{0,1}\d{0,2})$/, ([, d]) => ({ "margin-right": `${d}px` })],
                [/^mt(\d+\.{0,1}\d{0,2})$/, ([, d]) => ({ "margin-top": `${d}px` })],
                [/^mb(\d+\.{0,1}\d{0,2})$/, ([, d]) => ({ "margin-bottom": `${d}px` })],
                [/^pd(\d+\.{0,1}\d{0,2})$/, ([, d]) => ({ "padding": `${d}px` })],
                [/^leh-(\d+\.{0,1}\d{0,2})$/, ([, d]) => ({ "line-height": `${d}px` })],
                [/^pdl(\d+\.{0,1}\d{0,2})$/, ([, d]) => ({ "padding-left": `${d}px` })],
                [/^pdr(\d+\.{0,1}\d{0,2})$/, ([, d]) => ({ "padding-right": `${d}px` })],
            ],
            transformers: [
                transformerDirectives(),
            ],
        })
    ],


    // css: {
    //   postcss: {
    //     plugins: [
    //       postcsspxtoviewport({
    //         unitToConvert: 'px', // 要转化的单位
    //         viewportWidth: 1920, // UI设计稿的宽度
    //         unitPrecision: 6, // 转换后的精度，即小数点位数
    //         propList: ['*'], // 指定转换的css属性的单位，*代表全部css属性的单位都进行转换
    //         viewportUnit: 'vw', // 指定需要转换成的视窗单位，默认vw
    //         fontViewportUnit: 'vw', // 指定字体需要转换成的视窗单位，默认vw
    //         selectorBlackList: ['ignore-'], // 指定不转换为视窗单位的类名，
    //         minPixelValue: 1, // 默认值1，小于或等于1px则不进行转换
    //         mediaQuery: true, // 是否在媒体查询的css代码中也进行转换，默认false
    //         replace: true, // 是否转换后直接更换属性值
    //         landscape: false // 是否处理横屏情况
    //       })
    //     ]
    //   }
    // },

    // server:{
    //   proxy:{
    //     '/api':{
    //       target:'http://8.130.12.162:28019/manage-api/v1',
    //       changeOrigin:true,
    //       rewrite:path => path.replace(/^\/api/,'')
    //     }
    //   }
    // }

    build: {
        outDir: "dist",
        minify: "esbuild",
        // esbuild 打包更快，但是不能去除 console.log，terser打包慢，但能去除 console.log
        // minify: "terser",
        // terserOptions: {
        // 	compress: {
        // 		drop_console: viteEnv.VITE_DROP_CONSOLE,
        // 		drop_debugger: true
        // 	}
        // },
        // 禁用 gzip 压缩大小报告，可略微减少打包时间
        reportCompressedSize: false,
        // 规定触发警告的 chunk 大小
        chunkSizeWarningLimit: 2000,
        rollupOptions: {
            output: {
                // 配置静态资源目录,可以解决assets目录下图片打包后无法访问的问题
                chunkFileNames: "assets/js/[name]-[hash].js",
                entryFileNames: "assets/js/[name]-[hash].js",
                assetFileNames: "assets/[ext]/[name]-[hash].[ext]"
            }
        }
    }
})
